﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KyersControl;
using Helper;
using SchoolManagementSystem.Controls;
using System.Threading;
namespace SchoolManagementSystem.Forms
{
    public partial class frmReportError : DevExpress.XtraEditors.XtraForm
    {
        HtmlEditor html = new HtmlEditor();
        Thread TsSend;
        Thread TSend;
        public frmReportError()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            html.Dock = DockStyle.Fill;
            pictureBox1.Visible = false;
            html.cmdimage.Visible = false;
            this.xtraScrollableControl1.Controls.Add(html);
        }
        string file = "";
        private void cmdselectfile_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog f = new OpenFileDialog();
            f.Multiselect = true;
            if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                foreach (var item in f.FileNames)
                {
                    file += item + ",";
                }
                file = file.Remove(file.Length - 1, 1);
            }
            if(file.Length>0)
            {
            foreach (var item in file.Split(','))
            {
                lblfilename.Text += System.IO.Path.GetFileName(item)+",";
            }
            lblfilename.Text = lblfilename.Text.Remove(lblfilename.Text.Length - 1, 1);
            }
        }

        private void cmdsend_Click(object sender, EventArgs e)
        {

            TsSend = new Thread(new ThreadStart(() => SendMail()));
            TsSend.Start();

        }

        void SendMail()
        {
           
            this.BeginInvoke((Action)(() =>
             {
                 pictureBox1.Visible = true;
                 cmdsend.Enabled = false;
                 string body = html.BodyHtml;
                 var user = RegisterService.RegisterServices.UserService.GetByID(ValueDefine.UserID) as CoreData.Users;
                 if (String.IsNullOrEmpty(user.Email))
                 {
                     MessageControl.ShowMessage("Bạn chưa cấu hình email\nvui lòng cầu hình email để tiếp tục", "", MessageControl.Icon.Info);
                     pictureBox1.Visible = false;
                     cmdsend.Enabled = true;
                     if (TsSend != null)
                     TsSend.Abort();
                     if (TSend != null)
                     TSend.Abort();
                     return;
                 }
                 try
                 {
                    
                     var files = file.Split(',');
                     string smtp = "";
                     int port = 587;
                     if (user.Email.Contains("gmail.com"))
                     {
                         smtp = "smtp.gmail.com";
                     }
                     if (user.Email.Contains("yahoo.com"))
                     {
                         smtp = "smtp.mail.yahoo.com";
                     }
                     if (user.Email.Contains("hotmail.com"))
                     {
                         smtp = "smtp.live.com";
                     }
                     TSend = new Thread(new ThreadStart(() => Send(user.Email, user.PassEmail, body, files, smtp, port)));
                     TSend.Start();
                     
                 }
                 catch
                 {
                     MessageControl.ShowMessage("Bị lỗi trong quá trình thực thi\nvui lòng thử lại", "Lỗi", MessageControl.Icon.Error);
                     pictureBox1.Visible = false;
                     cmdsend.Enabled = true;
                     if (TsSend != null)
                     TsSend.Abort();
                     if (TSend != null)
                     TSend.Abort();
                   
                 }

             }));
            
        }

        void Send(string email,string pass,string body, string[]files, string smtp, int port)
        {
            var IsSend = EmailHelper.Send("duylinh191@gmail.com", email, Security.DecryptString(pass), "Jbart - " + ValueDefine.UserNname, "Jbart - " + txttitle.Text, body, files, smtp, port);
            if (IsSend)
            {
                pictureBox1.Visible = false;
                cmdsend.Enabled = true;
                MessageControl.ShowMessage("Report của bạn đã được gửi đi\nchúng tôi sẽ phản hồi trong vòng 24h", "", MessageControl.Icon.Success);
                if(TsSend!=null)
                    TsSend.Abort();
                if(TSend!=null)
                    TSend.Abort();
                
               
            }
            else
            {
                pictureBox1.Visible = false;
                cmdsend.Enabled = true;
                MessageControl.ShowMessage("Bị lỗi trong quá trình thực thi\nvui lòng thử lại", "Lỗi", MessageControl.Icon.Error);
                if (TsSend != null)
                TsSend.Abort();
                if (TSend != null)
                TSend.Abort();
              
            }
        }

    }
}
